<script setup>
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelSelect from './components/ChannelSelect.vue'
import ArticleEdit from './components/ArticleEdit.vue'
import { artGetListService, artDeleteService } from '@/api/article'
import { formatTime } from '@/utils/format'
const size = ref('small')
const artManageList = ref([])
const loading = ref(false)
const total = ref(0)
const articleEdit = ref()
const params = ref({
  pagenum: 1,
  pagesize: 6,
  cate_id: '',
  state: ''
})
const getartList = async () => {
  loading.value = true
  const res = await artGetListService(params.value)
  artManageList.value = res.data.data
  total.value = res.data.total
  loading.value = false
}
getartList()
const onCurrentChange = (value) => {
  params.value.pagenum = value
  getartList()
}

const onSizeChange = (value) => {
  params.value.pagenum = 1
  params.value.pagesize = value
  getartList()
}

const onSearch = () => {
  params.value.pagenum = 1
  getartList()
}
const onReset = () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getartList()
}
const addArticle = () => {
  articleEdit.value.open({})
}
const onEdit = (row) => {
  articleEdit.value.open(row)
}
const onSuccess = (type) => {
  if (type === 'add') {
    // console.log();
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    params.value.pagenum = lastPage
  }
  getartList()
}
const onDelete = async (row) => {
  await ElMessageBox.confirm('你确认删除该分类信息吗？', '温馨提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    await artDeleteService(row.id)
    ElMessage({
      type: 'success',
      message: '删除成功'
    })
  })
  getartList()
}
</script>

<template>
  <PageContainer title="文章管理">
    <template #extra>
      <el-button type="primary" @click="addArticle">添加文章</el-button>
    </template>
    <!-- 表单区域 -->
    <el-form inline :size="size">
      <el-form-item label="文章分类：">
        <ChannelSelect v-model="params.cate_id"></ChannelSelect>
      </el-form-item>
      <el-form-item label="发布状态：">
        <el-select style="width: 200px" v-model="params.state">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格区域 -->
    <el-table :data="artManageList" style="width: 100%" v-loading="loading">
      <el-table-column label="文章标题" prop="title" wdith="400">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="cate_name"></el-table-column>
      <el-table-column label="发布时间" prop="pub_date">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="state"></el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button
            @click="onEdit(row)"
            plain
            circle
            type="primary"
            :icon="Edit"
          ></el-button>
          <el-button
            plain
            circle
            type="danger"
            :icon="Delete"
            @click="onDelete(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[2, 4, 6, 8]"
      :size="size"
      :background="true"
      layout="jumper,total, sizes, prev, pager, next"
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
    <ArticleEdit ref="articleEdit" @success="onSuccess"></ArticleEdit>
  </PageContainer>
</template>
<style></style>
